package com.lxmes.sales.service.impl;

import java.util.List;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.domain.NcSupplier;
import com.lxmes.base.mapper.NcSupplierMapper;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.JcIteminfoPo;
import com.lxmes.sales.domain.NcSupplierMaterial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.NcPurchaseFormalPlanMapper;
import com.lxmes.sales.domain.NcPurchaseFormalPlan;
import com.lxmes.sales.service.INcPurchaseFormalPlanService;

/**
 * 采购正式计划Service业务层处理
 *
 * @author lx
 * @date 2023-06-06
 */
@Service
public class NcPurchaseFormalPlanServiceImpl implements INcPurchaseFormalPlanService
{
    @Autowired
    private NcPurchaseFormalPlanMapper ncPurchaseFormalPlanMapper;

    @Autowired
    private NcSupplierMapper ncSupplierMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询采购正式计划
     *
     * @param id 采购正式计划主键
     * @return 采购正式计划
     */
    @Override
    public NcPurchaseFormalPlan selectNcPurchaseFormalPlanById(Long id)
    {
        return ncPurchaseFormalPlanMapper.selectNcPurchaseFormalPlanById(id);
    }

    /**
     * 查询采购正式计划列表
     *
     * @param ncPurchaseFormalPlan 采购正式计划
     * @return 采购正式计划
     */
    @Override
    public List<NcPurchaseFormalPlan> selectNcPurchaseFormalPlanList(NcPurchaseFormalPlan ncPurchaseFormalPlan)
    {
        return ncPurchaseFormalPlanMapper.selectNcPurchaseFormalPlanList(ncPurchaseFormalPlan);
    }

    @Override
    public List<NcPurchaseFormalPlan> selectNcPurchaseFormalPlanBySupplierCode(NcPurchaseFormalPlan ncPurchaseFormalPlan)
    {
        return ncPurchaseFormalPlanMapper.selectNcPurchaseFormalPlanBySupplierCode(ncPurchaseFormalPlan);
    }

    /**
     * 新增采购正式计划
     *
     * @param ncPurchaseFormalPlan 采购正式计划
     * @return 结果
     */
    @Override
    public int insertNcPurchaseFormalPlan(NcPurchaseFormalPlan ncPurchaseFormalPlan)
    {

        List<JcIteminfoPo> jcIteminfoList = ncPurchaseFormalPlan.getJcIteminfoList();
        for(JcIteminfoPo jcIteminfo : jcIteminfoList){
            ncPurchaseFormalPlan.setMaterialType(jcIteminfo.getClassName());
            ncPurchaseFormalPlan.setMaterialNumber(jcIteminfo.getItemCode());
            ncPurchaseFormalPlan.setMaterialSubnumber(jcIteminfo.getItemId());
            ncPurchaseFormalPlan.setMaterialName(jcIteminfo.getItemName());
            ncPurchaseFormalPlan.setSpecification(jcIteminfo.getSpec());
            ncPurchaseFormalPlan.setMaterialUnit(jcIteminfo.getUnit());
            ncPurchaseFormalPlan.setBarcode(jcIteminfo.getTmh());
            NcSupplier ncSupplier = ncSupplierMapper.selectNcSupplierById(jcIteminfo.getSupplyId());
            ncPurchaseFormalPlan.setSupplierCode(ncSupplier.getSupplierCode());
            ncPurchaseFormalPlan.setSupplierName(ncSupplier.getSupplierName());

            ncPurchaseFormalPlan.setCustodyAttribute(jcIteminfo.getIsTuoguan());
            ncPurchaseFormalPlan.setAnnualPurchasePlanQuantity(jcIteminfo.getYhNum());
            ncPurchaseFormalPlan.setPlannedPrice(jcIteminfo.getJhUnitPrice());
            ncPurchaseFormalPlan.setPlannedAmount(jcIteminfo.getJhTotalPrice());
            ncPurchaseFormalPlan.setTaxRate(jcIteminfo.getTaxRate());
            ncPurchaseFormalPlan.setInclusivePurchasePrice(jcIteminfo.getInclusivePurchasePrice());
            ncPurchaseFormalPlan.setInclusivePurchaseAmount(jcIteminfo.getInclusivePurchaseAmount());
            ncPurchaseFormalPlan.setExclusivePurchasePrice(jcIteminfo.getExclusivePurchasePrice());
            ncPurchaseFormalPlan.setExclusivePurchaseAmount(jcIteminfo.getExclusivePurchaseAmount());
            ncPurchaseFormalPlan.setPackagingCost(jcIteminfo.getPackagingCost());
            ncPurchaseFormalPlan.setShippingCost(jcIteminfo.getShippingCost());
            ncPurchaseFormalPlan.setLogisticsCost(jcIteminfo.getLogisticsCost());
            ncPurchaseFormalPlan.setMaterial(jcIteminfo.getMaterial());
            ncPurchaseFormalPlan.setPackagingMaterial(jcIteminfo.getPackagingMaterial());
            ncPurchaseFormalPlan.setPackagingSize(jcIteminfo.getPackagingSize());
            ncPurchaseFormalPlan.setSingleBoxWeight(jcIteminfo.getSingleWeight());

            ncPurchaseFormalPlan.setCreateTime(DateUtils.getNowDate());
            ncPurchaseFormalPlan.setCreateBy(SecurityUtils.getUsername());
            ncPurchaseFormalPlanMapper.insertNcPurchaseFormalPlan(ncPurchaseFormalPlan);
        }
        return 1;
    }

    /**
     * 修改采购正式计划
     *
     * @param ncPurchaseFormalPlan 采购正式计划
     * @return 结果
     */
    @Override
    public int updateNcPurchaseFormalPlan(NcPurchaseFormalPlan ncPurchaseFormalPlan)
    {
        ncPurchaseFormalPlan.setUpdateTime(DateUtils.getNowDate());
        return ncPurchaseFormalPlanMapper.updateNcPurchaseFormalPlan(ncPurchaseFormalPlan);
    }

    /**
     * 批量删除采购正式计划
     *
     * @param ids 需要删除的采购正式计划主键
     * @return 结果
     */
    @Override
    public int deleteNcPurchaseFormalPlanByIds(Long[] ids)
    {
        return ncPurchaseFormalPlanMapper.deleteNcPurchaseFormalPlanByIds(ids);
    }

    /**
     * 删除采购正式计划信息
     *
     * @param id 采购正式计划主键
     * @return 结果
     */
    @Override
    public int deleteNcPurchaseFormalPlanById(Long id)
    {
        return ncPurchaseFormalPlanMapper.deleteNcPurchaseFormalPlanById(id);
    }
    @Override
    public String imporIteminfo(List<NcPurchaseFormalPlan> ncPurchaseFormalPlanList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncPurchaseFormalPlanList) || ncPurchaseFormalPlanList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcPurchaseFormalPlan ncPurchaseFormalPlan : ncPurchaseFormalPlanList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncPurchaseFormalPlan);
                ncPurchaseFormalPlan.setCreateBy(operName);
                ncPurchaseFormalPlan.setUpdateBy(operName);
                this.insertNcPurchaseFormalPlan(ncPurchaseFormalPlan);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<NcPurchaseFormalPlan> getNcPurchaseFormalPlanByPlanNumber(NcPurchaseFormalPlan ncPurchaseFormalPlan) {
        return ncPurchaseFormalPlanMapper.getNcPurchaseFormalPlanByPlanNumber(ncPurchaseFormalPlan);
    }
}
